package com.shenyue.nbtownlet.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.util.PermissionsDispatcher;
import com.trycatch.mysnackbar.Prompt;
import com.trycatch.mysnackbar.TSnackbar;

public class BaseActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                d();
            }
        }, 320);
    }

    private void d() {
        PermissionsDispatcher.initPermissionsDispatcher(this);
    }

    public void setActionBar(boolean back, String title) {
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar mActionBar = getBaseActionBar();
        //去掉底部阴影
        //mActionBar.setElevation(0);
        if (back) {
            mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
        }
        toolbar.setTitle(title);
    }

    public Toolbar getToolbar(){
        return toolbar;
    }

    public void setActionBarTitle(String title) {
        toolbar.setTitle(title);
    }

    public ActionBar getBaseActionBar() {
        return getSupportActionBar();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void toActivity(final Class<?> cls) {
        toActivity(cls, false);
    }

    public void toActivity(final Class<?> cls, final boolean closeFlag) {
        try {
            final Context mContext = this;
            Intent intent = new Intent();
            intent.setClass(mContext, cls);
            startActivity(intent);
            if (closeFlag)
                finish();
        } catch (Exception e) {

        }
    }

    public void toActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        try {
            Intent intent = new Intent();
            intent.setClass(this, cls);
            if (bundle != null)
                intent.putExtras(bundle);
            startActivityForResult(intent, requestCode);
        } catch (Exception e) {

        }
    }

    public void toActivity(final Class<?> cls, final Bundle bundle, final boolean closeFlag) {
        try {
            final Context mContext = this;
            Intent intent = new Intent();
            intent.setClass(mContext, cls);
            if (bundle != null)
                intent.putExtras(bundle);
            startActivity(intent);
            if (closeFlag)
                finish();
        } catch (Exception e) {

        }
    }

    public void toActivity(final Class<?> cls, final Bundle bundle) {
        toActivity(cls, bundle, false);
    }

    public void showMsg(String msg) {
        View view = findViewById(R.id.container);
        TSnackbar.make(view, msg, TSnackbar.LENGTH_SHORT, TSnackbar.APPEAR_FROM_TOP_TO_DOWN).setPromptThemBackground(Prompt.WARNING).show();
    }

    public void showMsgS(String msg) {
        View view = findViewById(R.id.container);
        TSnackbar.make(view, msg, TSnackbar.LENGTH_SHORT, TSnackbar.APPEAR_FROM_TOP_TO_DOWN).setPromptThemBackground(Prompt.SUCCESS).show();
    }

    public TSnackbar showTSnackbar(String msg) {
        View view = findViewById(R.id.container);
        final TSnackbar snackBar = TSnackbar.make(view, msg, TSnackbar.LENGTH_INDEFINITE, TSnackbar.APPEAR_FROM_TOP_TO_DOWN);
        snackBar.setPromptThemBackground(Prompt.SUCCESS);
        snackBar.addIconProgressLoading(0, true, false);
        snackBar.show();
        return snackBar;
    }

    public void hideTSnackbar(TSnackbar snackBar, String msg, Prompt prompt) {
        if (snackBar == null)
            return;
        snackBar.setDuration(TSnackbar.LENGTH_SHORT);
        snackBar.setText(msg);
        snackBar.setPromptThemBackground(prompt);
        snackBar.show();
    }

    public void hideTSnackbar(TSnackbar snackBar, String msg) {
        if (snackBar == null)
            return;
        snackBar.setDuration(TSnackbar.LENGTH_SHORT);
        snackBar.setText(msg);
        snackBar.setPromptThemBackground(Prompt.SUCCESS);
        snackBar.show();
    }

    public void showToast(String message){
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
